package Application.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import Application.dao.StudentDao;
import Application.entity.Result;
import Application.entity.Student;
import Application.entity.StudentVo;
import Application.service.StudentService;

@Service("StudentService")
public class StudentServiceImpl implements StudentService {

	@Resource
	private StudentDao dao;

	@Override
	public Result insertStudent(Student student) {
		Result r = new Result();
		try {
			dao.insertStudent(student);
			r.setContent(1);
		} catch (Exception e) {
			r.setContent(0);
		}
		return r;

	}

	@Override
	public List<Student> findStudentList() {
		return dao.findStudentList();
	}

	@Override
	public Student findStudentById(int id) {
		return dao.findStudentById(id);
	}

	@Override
	public Result checklog(Student student) {
		Result r = new Result();
		try {
			List<Student> list=dao.findStudentList();
			r.setContent(0);
			for(Student s :list){
				if(s.getId()==student.getId()&&s.getPassword()==student.getPassword()) {
					r.setContent(1);
				}
			}
		} catch (Exception e) {
			r.setContent(0);
		}
		return r;
	}

	@Override
	public Result update(StudentVo student) {
		Result r = new Result();
		try {
			Student s=dao.findStudentById(student.getId());
			if(s.getPassword()==student.getOldpassword()) {
				dao.updateStudent(student);
				r.setContent(1);
			}else {
				r.setContent(0);
			}
		} catch (Exception e) {
			r.setContent(0);
		}
		return r;
	}

	@Override
	public List<Student> findMatesByStudentId(int id) {
		Student s=dao.findStudentById(id);
		return dao.findMatesByClas(s.getClas()); 
	}


}
